package com.apps.sworld.dao;


import com.apps.sexctption.DataException;
import com.apps.sexctption.NullException;
import com.apps.shelper.ClassHelper;
import com.apps.sutils.StringUtils;
import com.apps.sworld.enums.TimeUnit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.apps.shelper.AnnotationHelper;
import com.apps.saspect.ValidMethod;
import com.apps.sannotation.constraints.NoNull;
import com.apps.sannotation.identity.ShopId;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 商城的商铺角色基类,是member的儿子，是所有商品可操作对象的父亲
 */
public interface sShop<T> extends sMember<T> {

    default String getShopIdName()
    {
        return AnnotationHelper.getAnnotationFieldName(ShopId.class,currentModelClass());
    }
  @ValidMethod
  default T getOneByShopId(@NoNull Integer shop_id)
  {
      return getOneByFieldValue(getShopIdName(),shop_id);
  }

    /**
     * 获取不到就创建
     * @param shop_id
     * @param t
     * @return
     */
    @ValidMethod
    default T getOneByShopId(@NoNull Integer shop_id,@NoNull T t)
    {
        T result=getOneByShopId(shop_id);
        if(null == result)
        {
            String filedName=AnnotationHelper.getAnnotationFieldName(ShopId.class,t.getClass());
            if(StringUtils.isEmpty(filedName))
                throw new NullException("店铺id字段不存在或没添加注解进行标识");
            Object filedValue= ClassHelper.getFiledValue(t,filedName);
            if(null== filedValue) {
                ClassHelper.setFiledValue(filedName, t, shop_id);
                filedValue=shop_id;
            }
            if(!filedValue.equals(shop_id))
                throw new DataException("模型的店铺id值必须等于传入的店铺id");

            return saveToDB(t);
        }
        return result;
    }

    @ValidMethod
    default void deleteByShopId(@NoNull Integer shop_id)
    {
        QueryWrapper<T> wrapper=new QueryWrapper<>();
        wrapper.eq(getShopIdName(),shop_id);
        delete(wrapper);
    }

    /**
     * 按店铺ID统计数据条数
     * @param shop_id
     * @return
     */
    @ValidMethod
    default int countByShopId(@NoNull Integer shop_id)
    {
        QueryWrapper<T> wrapper=new QueryWrapper<>();
        wrapper.eq(getShopIdName(),shop_id);
        return count(wrapper);
    }

  @ValidMethod
  default boolean isEmptyByShopId(@NoNull Integer shopid)
  {
      QueryWrapper<T> wrapper=new QueryWrapper<>();
      wrapper.eq(getShopIdName(),shopid);
      return isEmpty(wrapper);
  }

  @ValidMethod
  default boolean isEmptyByShopId(@NoNull Integer shopid,@NoNull QueryWrapper<T> wrapper)
  {
      wrapper.eq(getShopIdName(),shopid);
      return isEmpty(wrapper);
  }
    /**
     * 按创建时间进行按年月日小时查询,传入的是字符串格式:2021090306 这样的格式
     * @param member_id
     * @param date
     * @param timeUnit
     * @return
     */
    @ValidMethod
    default List<T> queryGmtCreateByShopId(@NoNull Integer member_id,@NoNull String date, @NoNull TimeUnit timeUnit)
    {
        QueryWrapper<T> wrapper=new QueryWrapper<>();
        wrapper.eq(getShopIdName(),member_id);
        return queryByGmtCreate(wrapper,date,timeUnit);
    }
    /**
     * 按创建时间进行按年月日小时分页查询,传入的是字符串格式:2021090306 这样的格式
     * @param member_id
     * @param date 传入的是字符串格式:2021090306
     * @param timeUnit
     * @return
     */
    @ValidMethod
    default IPage<T> pageQueryGmtCreateByShopId(@NoNull Integer member_id,@NoNull String date, @NoNull TimeUnit timeUnit,int pageNo, int pageSize)
    {
        QueryWrapper<T> wrapper=new QueryWrapper<>();
        wrapper.eq(getShopIdName(),member_id);
        return pageQueryByGmtCreate(wrapper,date,timeUnit,pageNo,pageSize);
    }
  @ValidMethod
  default IPage<T> pageQueryByShopId(@NoNull Integer shopid, int pageNo, int pageSize)
  {
      QueryWrapper<T> wrapper=new QueryWrapper<>();
      wrapper.eq(getShopIdName(),shopid);
      return pageQuery(wrapper,pageNo,pageSize);
  }

    @ValidMethod
    default IPage<T> pageQueryByShopId(@NoNull Integer shopid, int pageNo, int pageSize,@NoNull QueryWrapper<T> wrapper)
    {
        wrapper.eq(getShopIdName(),shopid);
        return pageQuery(wrapper,pageNo,pageSize);
    }

    @ValidMethod
    default List<T> queryByShopId(@NoNull Integer shop_id)
    {
        return queryByFieldValue(getShopIdName(),shop_id);
    }

    @ValidMethod
    default List<T> queryByShopId(@NoNull Integer shopid,@NoNull QueryWrapper<T> wrapper)
    {
        wrapper.eq(getShopIdName(),shopid);
        return query(wrapper);
    }

    /**
     * 按时间区段分页查询
     * @param shop_id
     * @param start
     * @param end
     * @param pageNo
     * @param pageSize
     * @return
     */
    @ValidMethod
    default IPage<T> pageQueryTimeIntervalByShopId(@NoNull Integer shop_id, @NoNull LocalDateTime start, @NoNull LocalDateTime end, int pageNo, int pageSize)
    {
        QueryWrapper<T> wrapper=new QueryWrapper<>();
        wrapper.eq(getCatIdName(),shop_id);
        return pageQueryTimeInterval(wrapper,start,end,pageNo,pageSize);
    }

    /**
     * 按时间区间查询
     * @param shop_id
     * @param start
     * @param end
     * @return
     */
    @ValidMethod
    default List<T> queryTimeIntervalByShopId(@NoNull Integer shop_id, @NoNull LocalDateTime start, @NoNull LocalDateTime end)
    {
        QueryWrapper<T> wrapper=new QueryWrapper<>();
        wrapper.eq(getCatIdName(),shop_id);
        return queryTimeInterval(wrapper,start,end);
    }

    /**
     * 按店铺id更新某个值
     * @param shop_id
     * @param fieldname
     * @param value
     * @return
     */
    @ValidMethod
    default T updateOneFieldByShopId(@NoNull Integer shop_id,@NoNull String fieldname,@NoNull Object value)
    {
        T t=getOneByShopId(shop_id);
        if(null == t)
            throw new NullException("shop_id为"+shop_id+"的记录不存在");
        ClassHelper.setFiledValue(fieldname,t,value);
        return updateById(t);
    }
}
